package com.example.stray_animal_management_system.admin.controller;

import com.example.stray_animal_management_system.commonu.service.HealthRecordService;
import com.example.stray_animal_management_system.entity.HealthRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: panpan
 * @time: 2025/11/1
 */
@RestController
@RequestMapping("/api/admin/health")
public class HealthAdminRecordController {

    @Autowired
    private HealthRecordService healthRecordService;

    /**
     * 查询健康档案（按动物ID）
     */
    @GetMapping("/list")
    public Map<String, Object> list(@RequestParam("animalId") Integer animalId) {
        Map<String, Object> res = new HashMap<>();
        List<HealthRecord> list = healthRecordService.lambdaQuery()
                .eq(HealthRecord::getAnimalId, animalId)
                .orderByDesc(HealthRecord::getCheckDate)
                .list();
        res.put("code", 200);
        res.put("data", list);
        return res;
    }

    /**
     * 添加健康记录
     */
    @PostMapping("/add")
    public Map<String, Object> add(@RequestBody HealthRecord record) {
        Map<String, Object> res = new HashMap<>();
        record.setCreatedAt(LocalDateTime.now());
        healthRecordService.save(record);
        res.put("code", 200);
        res.put("msg", "添加成功");
        return res;
    }

    /**
     * 修改健康记录
     */
    @PutMapping("/update")
    public Map<String, Object> update(@RequestBody HealthRecord record) {
        Map<String, Object> res = new HashMap<>();
        healthRecordService.updateById(record);
        res.put("code", 200);
        res.put("msg", "修改成功");
        return res;
    }

    /**
     * 删除健康记录
     */
    @DeleteMapping("/delete/{id}")
    public Map<String, Object> delete(@PathVariable Integer id) {
        Map<String, Object> res = new HashMap<>();
        healthRecordService.removeById(id);
        res.put("code", 200);
        res.put("msg", "删除成功");
        return res;
    }
}
